package com.appsindy.common.util;

import java.text.DecimalFormat;

public class NumberUtil {
    
    private NumberUtil(){}

    public static String getNumberStr(double value, int precision){
        String formatStr = "#,##0";
        if(precision > 0){
            formatStr += ".";
            for(int i = 0; i < precision; i++){
                formatStr += "0";
            }
        }
        DecimalFormat decFormat = new DecimalFormat(formatStr);
        
        return decFormat.format(value);
    }
    
    public static String toNumberStr(double value, int precision){
        String formatStr = "###0";
        if(precision > 0){
            formatStr += ".";
            for(int i = 0; i < precision; i++){
                formatStr += "0";
            }
        }
        DecimalFormat decFormat = new DecimalFormat(formatStr);
        
        return decFormat.format(value);
    }    
    
    public static String getCurrencyFormat(double number){
    	DecimalFormat currencyFormat = new DecimalFormat();
        currencyFormat.setDecimalSeparatorAlwaysShown(true);
        currencyFormat.setMaximumFractionDigits(6);
        currencyFormat.setMinimumFractionDigits(4);
        
        return currencyFormat.format(number);
    }
    
    public static String getCurrencyFormat(long number){
    	DecimalFormat currencyFormat = new DecimalFormat();
        currencyFormat.setDecimalSeparatorAlwaysShown(true);
        currencyFormat.setMaximumFractionDigits(2);
        currencyFormat.setMinimumFractionDigits(2);
       
        return currencyFormat.format(number);
    }
    
    public static String getIntFormatComma(double number){
    	DecimalFormat intFormatComma = new DecimalFormat();
        intFormatComma.setDecimalSeparatorAlwaysShown(false);
        intFormatComma.setGroupingUsed(true);
        intFormatComma.setGroupingSize(3);
      
        return intFormatComma.format(number);
    }
    
    public static String getIntFormatComma(long number){
    	DecimalFormat intFormatComma = new DecimalFormat();
        intFormatComma.setDecimalSeparatorAlwaysShown(false);
        intFormatComma.setGroupingUsed(true);
        intFormatComma.setGroupingSize(3);
      
        return intFormatComma.format(number);
    }
    
    public static String getThreeDigitFormat(double number){
    	DecimalFormat threeDigitFormat = new DecimalFormat();
    	threeDigitFormat.setDecimalSeparatorAlwaysShown(true);
    	threeDigitFormat.setMaximumFractionDigits(3);
    	threeDigitFormat.setMinimumFractionDigits(3);
        
	return threeDigitFormat.format(number);
    }
    
    public static String getThreeDigitFormat(long number){
    	DecimalFormat threeDigitFormat = new DecimalFormat();
    	threeDigitFormat.setDecimalSeparatorAlwaysShown(true);
    	threeDigitFormat.setMaximumFractionDigits(3);
    	threeDigitFormat.setMinimumFractionDigits(3);
       
	return threeDigitFormat.format(number);
    }
    
    public static String getIntFormat(double number){
    	DecimalFormat intFormat = new DecimalFormat();
        intFormat.setDecimalSeparatorAlwaysShown(false);
        intFormat.setGroupingUsed(false);
        
        return intFormat.format(number);
    }
    public static String getIntFormat(long number){
        
    	DecimalFormat intFormat = new DecimalFormat();
        intFormat.setDecimalSeparatorAlwaysShown(false);
        intFormat.setGroupingUsed(false);
        return intFormat.format(number);
    }
    
    public static boolean validateNumber(String num) {
    	try {
            Integer.parseInt(num);
            return true;
    	} catch (Exception e) {
            return false;
    	}
    }
    
    public static double roundUpCurrency(double Rval, int Rpl) {
        double p = (double)Math.pow(10,Rpl);
        Rval = Rval * p;
        double tmp = Math.round(Rval);
        return (double)tmp/p;
    }
    
    public static long getRoundUnit(double numberOfUnit, int unit) {
        if(numberOfUnit % unit == 0){
            return (long)numberOfUnit;
        }
        return (((long)numberOfUnit) - (((long)numberOfUnit) % unit)) + unit;
    }
    
    public static void main(String args[]){
        //System.out.println(toNumberStr(120000000.00, 2));
        //System.out.println("getRoundUnit = " + getRoundUnit(1235001.00, 1000));
        System.out.println("toNumberStr = " + toNumberStr(120000000.1234, 6));
    }
    
}
